package com.golddaymicromall.activity.storemanager;

import android.os.Handler;
import android.os.Bundle;
import android.os.Message;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.golddaymicromall.activity.R;
import com.golddaymicromall.base.BaseActivity;
import com.golddaymicromall.bean.TePinInfo;
import com.golddaymicromall.util.ActivityController;
import com.golddaymicromall.util.RopUtils;
import com.golddaymicromall.util.SPUtils;
import com.golddaymicromall.util.ServerConfig;
import com.google.common.collect.Maps;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.convert.StringConvert;
import com.lzy.okrx.RxAdapter;

import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import rx.android.schedulers.AndroidSchedulers;

public class ShengQingTePinActivity extends BaseActivity implements View.OnClickListener {


    TextView mtitle;
    TextView mshenqing_neirong;
    Button magree;
    Button close;
    List<TePinInfo> list;
    String customerid;
    ProgressBar progress;
    String text;
    String OrganizationId;
    LinearLayout back;

    private Handler mOffHandler;
    private Timer mOffTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sheng_qing_te_pin);
        customerid = SPUtils.get(getApplicationContext(), "Id", "").toString();
        OrganizationId = SPUtils.get(getApplicationContext(), "OrganizationId", "").toString();
        magree = (Button) findViewById(R.id.agree);
        back = (LinearLayout) findViewById(R.id.activity_new_add_btn_back);
        close = (Button) findViewById(R.id.close);
        mshenqing_neirong = (TextView) findViewById(R.id.shenqing_neirong);
        progress = (ProgressBar) findViewById(R.id.progress);
        mtitle = (TextView) findViewById(R.id.title);
        magree.setOnClickListener(this);
        back.setOnClickListener(this);
        close.setOnClickListener(this);
        if ("29".equals(OrganizationId)) {
            magree.setVisibility(View.GONE);
            close.setVisibility(View.GONE);
        }
        attemptgetinfo();
        attemptShengtepin();
        timeCountDialog();
    }

    private void timeCountDialog() {
        mOffHandler = new Handler() {
            public void handleMessage(Message msg) {
                if (msg.what > 0) {
                    //动态显示倒计时
                    magree.setText(msg.what + "秒");
                } else {
                    magree.setText("自愿申请");
                    mOffTime.cancel();
                }
                super.handleMessage(msg);
            }
        };
        //////倒计时
        mOffTime = new Timer(true);
        TimerTask tt = new TimerTask() {
            int countTime = 10;

            public void run() {
                if (countTime > 0) {
                    countTime--;
                }
                Message msg = new Message();
                msg.what = countTime;
                mOffHandler.sendMessage(msg);
            }
        };
        mOffTime.schedule(tt, 1000, 1000);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.agree:
                if (magree.getText().toString().contains("自愿申请")) {
                    attemptpossinfo();
                }
                break;
            case R.id.activity_new_add_btn_back:
                ActivityController.getInstance().removeActivity(this);
                finish();
                break;
            case R.id.close:
                ActivityController.getInstance().removeActivity(this);
                finish();
                break;
            default:
                break;
        }
    }

    //申请特聘
    private void attemptShengtepin() {
        //获取本地数据
        String logid = SPUtils.get(getApplicationContext(), "Id", "").toString();
        Map<String, String> form = Maps.newHashMap();
        form.put("method", "rop.customer.oristg");
        form.put("appKey", "00001");
        form.put("v", "1.0");
        form.put("format", "json");
        form.put("logid", logid);
        String sing = RopUtils.signString(form, "qwertyuiop");
        form.put("sign", sing);

        OkGo.post(new ServerConfig().SERVER)
                .params(form)
                .getCall(StringConvert.create(), RxAdapter.<String>create())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(result -> {
                    if (!"".equals(result)) {
                        if (result.contains("errorToken") || "0".equals(result)) {
                            Toast.makeText(getApplicationContext(), "访问服务器失败,请稍后重试", Toast.LENGTH_SHORT).show();
                        } else if ("null".equals(result)) {
                            Toast.makeText(getApplicationContext(), "暂无数据", Toast.LENGTH_SHORT).show();
                        } else {
                            if (Integer.parseInt(result) == 1) {
                            }
                        }
                    }
                }, throwable -> {
                    showError();
                    throwable.printStackTrace();
                });

    }

    public void attemptgetinfo() {
        Map<String, String> form = Maps.newHashMap();
        form.put("method", "rop.notice.getxy");
        form.put("appKey", "00001");
        form.put("v", "1.0");
        form.put("format", "json");
        String sing = RopUtils.signString(form, "qwertyuiop");
        form.put("sign", sing);

        OkGo.post(new ServerConfig().SERVER)
                .params(form)
                .getCall(StringConvert.create(), RxAdapter.<String>create())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(result -> {
                    if (!"".equals(result)) {
                        if (result.contains("errorToken") || "0".equals(result)) {
                            Toast.makeText(ShengQingTePinActivity.this, "访问服务器失败,请稍后重试", Toast.LENGTH_SHORT).show();
                            magree.setVisibility(View.INVISIBLE);
                        } else if ("null".equals(result)) {
                            Toast.makeText(ShengQingTePinActivity.this, "暂无数据", Toast.LENGTH_SHORT).show();
                            magree.setVisibility(View.INVISIBLE);
                        } else {
                            list = JSON.parseArray(result, TePinInfo.class);
                            String content = list.get(0).getContent();
                            String title = list.get(0).getTitle();
                            text = Html.fromHtml(content).toString();
                            mshenqing_neirong.setText(text);

                        }
                    } else {
                        Toast.makeText(ShengQingTePinActivity.this, "没有请求到数据,请稍后重试", Toast.LENGTH_SHORT).show();
                    }
                }, throwable -> {
                    showError();
                    throwable.printStackTrace();
                });

    }

    public void attemptpossinfo() {
        Map<String, String> form = Maps.newHashMap();
        form.put("method", "rop.customer.istg");
        form.put("appKey", "00001");
        form.put("v", "1.0");
        form.put("format", "json");
        form.put("customerclassid", "29");
        form.put("logid", customerid);
        String sing = RopUtils.signString(form, "qwertyuiop");
        form.put("sign", sing);

        OkGo.post(new ServerConfig().SERVER)
                .params(form)
                .getCall(StringConvert.create(), RxAdapter.<String>create())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(result -> {
                    if (Integer.parseInt(result) == 1) {
                        Toast.makeText(ShengQingTePinActivity.this, "申请特聘成功", Toast.LENGTH_SHORT).show();
                        SPUtils.put(ShengQingTePinActivity.this, "OrganizationId", "29");
                        finish();
                    } else if ("-2".equals(result)) {
                        Toast.makeText(ShengQingTePinActivity.this, "尚未有申请资格", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(ShengQingTePinActivity.this, "申请特聘失败", Toast.LENGTH_SHORT).show();
                    }
                }, throwable -> {
                    showError();
                    throwable.printStackTrace();
                });

    }
}
